package com.hlxt.nj.busi.action;

import com.alibaba.fastjson.JSON;
import com.hlxt.nj.busi.model.TCourse;
import com.hlxt.nj.busi.model.TUserCou;
import com.hlxt.nj.busi.model.TUserCouCriteria;
import com.hlxt.nj.busi.service.CourseService;
import com.hlxt.nj.busi.service.UserCouService;
import com.hlxt.nj.common.*;
import com.hlxt.nj.sys.action.BaseAction;
import com.hlxt.nj.sys.model.TUser;
import com.hlxt.nj.sys.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/choose")
public class ChooseAction extends BaseAction {


    @Autowired
    private UserCouService userCouService;

    @Autowired
    private TUserService userService;

    @Autowired
    private CourseService courseService;

    /**
     *
     * 查询每个课程选择的人
     */
    @ResponseBody
    @RequestMapping("/queryEvery.do")
    public String queryEvery(String cid) {
        if(StringUtil.isEmpty(cid)){
           log.info("请选择课程");
        }
            PageBean pageBean = getPageBean();
            TUserCouCriteria userCouCriteria = new TUserCouCriteria();
            TUserCouCriteria.Criteria criteria = userCouCriteria.createCriteria();
            criteria.andCidEqualTo(cid);
            userCouService.selectPageBeanByExample(userCouCriteria,pageBean);

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("rows", pageBean.getResults());
            map.put("total", pageBean.getTotalRecords());
            String json = JSON.toJSONString(map);
            return json;



    }

    @RequestMapping("/xuanze.do")
    @ResponseBody
    public RespMessage xuanze(String cid,String uid) {

        if(!StringUtil.isEmpty(cid,uid)){

            PageBean pageBean = getPageBean();
            TUserCouCriteria userCouCriteria = new TUserCouCriteria();
            TUserCouCriteria.Criteria criteria = userCouCriteria.createCriteria();
                criteria.andCidEqualTo(cid);
                criteria.andSidEqualTo(uid);
                userCouService.selectPageBeanByExample(userCouCriteria,pageBean);
                List<TUserCou> list = pageBean.getResults();
                if(list.size()>0){
                    log.info("选择课程失败,已经选择过");
                    return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
                }else{
                    TCourse course = new TCourse();
                    course = courseService.selectByPrimaryKey(cid);
                    int count = course.getCount();
                    int maxpop = course.getMaxpop();
                    if(count<maxpop){
                        TUserCou tUserCou =new TUserCou();
                        tUserCou.setCid(cid);
                        tUserCou.setSid(uid);
                        boolean flag = userCouService.insert(tUserCou);
                        if (flag) {
                            //更新选课数量
                            count++;
                            course.setId(cid);
                            course.setCount(count);
                            System.out.println("count+"+count);
                            courseService.updateByPrimaryKey(course);
                            //更新学生选课数量
                            TUser user = new TUser();
                            user = userService.getByUserid(uid);
                            int ucount = user.getCount();
                            ucount++;
                            user.setUsrid(uid);
                            user.setCount(ucount);
                            userService.update(user);
                            log.info("添加信息成功");
                            return RespMessageUtil.getResp(RetCodeDef.SUCCESS, "");

                        } else {
                            log.info("添加信息失败,SQL异常");
                            return RespMessageUtil.getResp(RetCodeDef.SQL_ERROR, "");
                        }

                    }else {
                        log.info("选择课程信息失败,人数已满");
                        return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
                    }
                }


        }else{
            log.info("选择课程信息失败,参数鉴权失败");
            return RespMessageUtil.getResp(RetCodeDef.PARA_AUTH_FAIL, "");
        }
    }

}
